home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / XCMDs and XFCNs / ExportText XFCN / ExportText.c < prev   
Encoding:
C/C++ Source or Header  |  1991-09-17  |  3.0 KB  |  144 lines  |  [TEXT/KAHL]

  1. #include <HyperXcmd.h>
  2.  
  3. /* 
  4.     ExportText XFCN
  5.     
  6.     Syntax:        ExportText( rsrc_id )
  7.     
  8.                 or
  9.                 
  10.                 ExportText( rsrc_id, file_path )
  11.     
  12.     Find TEXT/styl on the clipboard and save it to a file as TEXT and
  13.     styl resources with resource ID rsrc_id.  If resources with that ID
  14.     already exist, they will be replaced.  In the former case, Standard File
  15.     is used to select the file.
  16.     
  17.     by James W. Walker
  18.         Internet        walkerj@math.scarolina.edu
  19.         CompuServe        76367,2271
  20.         America Online    JWWalker
  21. */
  22.  
  23.  
  24. pascal void main( XCmdPtr param_ptr );
  25. void Return_string( XCmdPtr param_ptr, StringPtr str );
  26.  
  27. #define        NIL                0L
  28.  
  29. pascal void main( XCmdPtr param_ptr )
  30. {
  31.     Str255    menu_title, work, path;
  32.     short    res_id;
  33.     long    l_res;
  34.     SFReply    reply;
  35.     Point    where;
  36.     short    refnum;
  37.     OSErr    err;
  38.     Handle    rsrc_h, text_h, styl_h;
  39.     long    data_len, offset;
  40.     
  41.     if ((param_ptr->paramCount < 1) || (param_ptr->paramCount > 2) )
  42.     {
  43.         Return_string(param_ptr, "\pThere must be 1 or 2 parameters.");
  44.         return;
  45.     }
  46.     
  47.     ZeroToPas( param_ptr, *(param_ptr->params[0]), work );
  48.     StringToNum( work, &l_res );
  49.     res_id = (short) l_res;
  50.     
  51.     /*
  52.         See if there are TEXT and styl data on the scrap.
  53.     */
  54.     text_h = NewHandle(0);
  55.     styl_h = NewHandle(0);
  56.     data_len = GetScrap( text_h, 'TEXT', &offset );
  57.     if (data_len < 0)
  58.     {
  59.         Return_string(param_ptr, "\pNo text on scrap!");
  60.         return;
  61.     }
  62.     data_len = GetScrap( styl_h, 'styl', &offset );
  63.     if (data_len < 0)
  64.     {
  65.         Return_string(param_ptr, "\pNo styled text on scrap!");
  66.         return;
  67.     }
  68.     
  69.  
  70.     /*
  71.         Next let's open the resource file.
  72.     */
  73.     switch (param_ptr->paramCount)
  74.     {
  75.         case 1:
  76.             where.h = where.v = 100;
  77.             SFGetFile( where, (StringPtr)"\p", NIL, -1, NIL, NIL, &reply );
  78.             if (!reply.good)
  79.             {
  80.                 Return_string(param_ptr, "\p");
  81.                 return;
  82.             }
  83.             err = SetVol( NIL, reply.vRefNum );
  84.             if (err != noErr)
  85.             {
  86.                 Return_string(param_ptr, "\pSetVol error");
  87.                 return;
  88.             }
  89.             refnum = OpenResFile( reply.fName );
  90.             break;
  91.         case 2:
  92.             ZeroToPas( param_ptr, *(param_ptr->params[1]), path );
  93.             refnum = OpenResFile( path );
  94.             break;
  95.     }
  96.     if (refnum == -1)
  97.     {
  98.         err = ResError();
  99.         NumToString( err, work );
  100.         Return_string(param_ptr, work );
  101.         return;
  102.     }
  103.     
  104.     /* 
  105.         If the file already has a TEXT/styl resources of the given ID,
  106.         we need to delete them.
  107.     */
  108.     rsrc_h =  Get1Resource( 'TEXT', res_id );
  109.     if (rsrc_h != NIL)
  110.     {
  111.         RmveResource( rsrc_h );
  112.         UpdateResFile( refnum );
  113.     }
  114.     rsrc_h =  Get1Resource( 'styl', res_id );
  115.     if (rsrc_h != NIL)
  116.     {
  117.         RmveResource( rsrc_h );
  118.         UpdateResFile( refnum );
  119.     }
  120.     
  121.     
  122.     /*
  123.         Add the new menu resource.
  124.     */
  125.     AddResource( text_h, 'TEXT', res_id, "\p" );
  126.     AddResource( styl_h, 'styl', res_id, "\p" );
  127.  
  128.     CloseResFile( refnum );
  129.     Return_string( param_ptr, "\p" );
  130. }
  131.  
  132. /* ----------------------------------------------------------------- */
  133. void Return_string( XCmdPtr param_ptr, StringPtr str )
  134. {
  135.     StringPtr    return_str;
  136.     
  137.     param_ptr->returnValue = NewHandle( str[0] + 1 );
  138.     if (param_ptr->returnValue == NIL)
  139.         return;
  140.     return_str = (StringPtr) *(param_ptr->returnValue);
  141.     BlockMove( &str[1], return_str, str[0] );
  142.     return_str[str[0]] = '\0';
  143. }
  144.